import 'package:flutter/material.dart';

//void main() {
//  runApp(MaterialApp(
//    home: AnimatedListDemo(),
//  ));
//}

class AnimatedListDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ListState();
  }
}

class _ListState extends State<AnimatedListDemo> {
  final List<String> _originalList = <String>["first", "second", "third"];
  List<String> _list;

  final GlobalKey<AnimatedListState> _listKey = GlobalKey();

  @override
  void initState() {
    _list = List.from(_originalList);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("animated list demo"),
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                _addItem(_list.length);
              }),
          IconButton(
            icon: Icon(Icons.delete),
            onPressed: () {
              _removeItem(_list.length - 1);
            },
          )
        ],
      ),
      body: AnimatedList(
        key: _listKey,
        itemBuilder: _getItem,
        initialItemCount: _list.length,
      ),
    );
  }

  _addItem(int index) {
    _listKey.currentState.insertItem(index);
    String content = index <= 2 ? _originalList[index] : index.toString();
    _list.add(content);
  }

  _removeItem(int index) {
    print("remove->${_list.length}");
    String item = _list.removeAt(index);
    _listKey.currentState.removeItem(index,
        (BuildContext context, Animation<double> animation) {
      print("remove callback->${_list.length}");
      return _getRemovedItem(item, index, animation);
    });
  }

  Widget _getItem(
      BuildContext context, int index, Animation<double> animation) {
    String content = index <= 2 ? _originalList[index] : index.toString();
    return _Item(animation, content, index);
  }

  Widget _getRemovedItem(
      String content, int index, Animation<double> animation) {
    return _Item(animation, content, index);
  }
}

class _Item extends StatefulWidget {
  final Animation<double> _animation;
  final String _content;
  final int _index;

  const _Item(this._animation, this._content, this._index);


  @override
  State<StatefulWidget> createState() {
    return _ItemState();
  }
}

class _ItemState extends State<_Item>{
  @override
  Widget build(BuildContext context) {
    return SizeTransition(
      sizeFactor: widget._animation,
      child: Container(
          color: widget._index.isEven ? Colors.green : Colors.blue,
          width: double.infinity,
          height: 100,
          child: Center(child: Text(widget._content))),
    );
  }
  @override
  void initState() {
    print("initState->${widget._content}");
    super.initState();
  }
  @override
  void dispose() {
    print("dispose->${widget._content}");
    super.dispose();
  }
  @override
  void didUpdateWidget(_Item oldWidget) {
    print("didUpdateWidget->${widget._content}--->${oldWidget._content}");
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    print("didChangeDependencies->${widget._content}");
    super.didChangeDependencies();
  }
}
